<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>TCP四次挥手详解</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        .step-number {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            width: 40px;
            height: 40px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 50%;
            font-weight: bold;
            box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
        }
        .timeline-line {
            background: linear-gradient(180deg, #667eea 0%, #764ba2 100%);
            width: 2px;
            position: absolute;
            left: 20px;
            top: 40px;
            bottom: -40px;
        }
        .first-letter {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: 0.2rem;
            font-weight: 700;
            color: #667eea;
        }
        .mermaid {
            background: white;
            border-radius: 12px;
            padding: 2rem;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
        }
        .icon-box {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            width: 60px;
            height: 60px;
            border-radius: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 24px;
            box-shadow: 0 8px 20px rgba(102, 126, 234, 0.3);
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6">TCP 四次挥手</h1>
                <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto">深入理解网络连接的优雅终止过程</p>
                <div class="mt-8 flex justify-center space-x-4">
                    <div class="bg-white bg-opacity-20 rounded-full px-6 py-2 backdrop-blur-sm">
                        <i class="fas fa-network-wired mr-2"></i>网络协议
                    </div>
                    <div class="bg-white bg-opacity-20 rounded-full px-6 py-2 backdrop-blur-sm">
                        <i class="fas fa-handshake mr-2"></i>连接管理
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12 card-hover">
            <div class="prose prose-lg max-w-none">
                <p class="text-gray-700 leading-relaxed text-lg">
                    <span class="first-letter">T</span>CP 的四次挥手过程通过四个步骤确保连接的可靠关闭。首先，主动关闭方发送 FIN 包请求关闭连接；其次，被动关闭方确认收到关闭请求并发送自己的 FIN 包；然后，主动关闭方确认收到被动关闭方的 FIN 包；最后，双方在 TIME-WAIT 状态下等待足够的时间以确保所有数据包都被正确处理。这个过程确保了数据传输的完整性和资源的正确释放。
                </p>
            </div>
        </div>

        <!-- Background Section -->
        <div class="grid md:grid-cols-2 gap-8 mb-12">
            <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                <div class="flex items-start mb-6">
                    <div class="icon-box mr-4">
                        <i class="fas fa-history"></i>
                    </div>
                    <div>
                        <h2 class="text-2xl font-bold text-gray-800 mb-2">四次挥手的背景</h2>
                        <div class="w-20 h-1 bg-gradient-to-r from-purple-500 to-indigo-500 rounded"></div>
                    </div>
                </div>
                <p class="text-gray-700 leading-relaxed">
                    TCP 是一种面向连接的协议，确保数据在发送和接收过程中按顺序到达并完整传输。在数据传输完成后，双方需要进行连接终止，以释放占用的资源。这时，四次挥手过程发挥了作用，确保双方都能正确关闭连接。
                </p>
            </div>

            <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                <div class="flex items-start mb-6">
                    <div class="icon-box mr-4">
                        <i class="fas fa-shield-alt"></i>
                    </div>
                    <div>
                        <h2 class="text-2xl font-bold text-gray-800 mb-2">为什么需要四次挥手</h2>
                        <div class="w-20 h-1 bg-gradient-to-r from-purple-500 to-indigo-500 rounded"></div>
                    </div>
                </div>
                <div class="space-y-4">
                    <div class="flex items-start">
                        <i class="fas fa-check-circle text-purple-600 mt-1 mr-3"></i>
                        <div>
                            <h4 class="font-semibold text-gray-800">确保数据完整性</h4>
                            <p class="text-gray-600 text-sm">确保双方都能确认已经完成所有数据的发送和接收</p>
                        </div>
                    </div>
                    <div class="flex items-start">
                        <i class="fas fa-check-circle text-purple-600 mt-1 mr-3"></i>
                        <div>
                            <h4 class="font-semibold text-gray-800">资源释放</h4>
                            <p class="text-gray-600 text-sm">双方逐步关闭连接，释放占用的资源</p>
                        </div>
                    </div>
                    <div class="flex items-start">
                        <i class="fas fa-check-circle text-purple-600 mt-1 mr-3"></i>
                        <div>
                            <h4 class="font-semibold text-gray-800">确保可靠性</h4>
                            <p class="text-gray-600 text-sm">防止连接被意外中断或不完全关闭</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Process Visualization -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12">
            <h2 class="text-3xl font-bold text-center mb-8 text-gradient">四次挥手过程可视化</h2>
            <div class="mermaid">
                sequenceDiagram
                    participant 客户端
                    participant 服务器
                    
                    Note over 客户端,服务器: 数据传输阶段
                    
                    客户端->>服务器: 第一次挥手：FIN=1, seq=u
                    Note right of 客户端: 进入 FIN-WAIT-1 状态
                    
                    服务器->>客户端: 第二次挥手：ACK=1, ack=u+1
                    Note left of 服务器: 进入 CLOSE-WAIT 状态
                    Note right of 客户端: 进入 FIN-WAIT-2 状态
                    
                    服务器->>客户端: 第三次挥手：FIN=1, seq=v
                    Note left of 服务器: 进入 LAST-ACK 状态
                    
                    客户端->>服务器: 第四次挥手：ACK=1, ack=v+1
                    Note right of 客户端: 进入 TIME-WAIT 状态
                    Note left of 服务器: 连接关闭
                    
                    Note right of 客户端: 等待 2MSL 后关闭
            </div>
        </div>

        <!-- Detailed Steps -->
        <div class="bg-white rounded-2xl shadow-xl p-8">
            <h2 class="text-3xl font-bold text-center mb-8 text-gradient">四次挥手详细步骤</h2>
            
            <div class="space-y-8">
                <!-- Step 1 -->
                <div class="relative">
                    <div class="timeline-line"></div>
                    <div class="flex items-start">
                        <div class="step-number mr-6">1</div>
                        <div class="flex-1 bg-gradient-to-r from-purple-50 to-indigo-50 rounded-xl p-6 card-hover">
                            <h3 class="text-xl font-bold text-gray-800 mb-3">
                                <i class="fas fa-paper-plane text-purple-600 mr-2"></i>
                                第一次挥手（主动关闭方发起）
                            </h3>
                            <div class="grid md:grid-cols-2 gap-4">
                                <div>
                                    <h4 class="font-semibold text-gray-700 mb-2">FIN 包发送</h4>
                                    <p class="text-gray-600">主动关闭方发送 FIN 包，表示数据传输完毕，请求终止连接。</p>
                                </div>
                                <div>
                                    <h4 class="font-semibold text-gray-700 mb-2">状态转换</h4>
                                    <p class="text-gray-600">主动关闭方进入 <span class="font-mono bg-purple-100 px-2 py-1 rounded">FIN-WAIT-1</span> 状态。</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Step 2 -->
                <div class="relative">
                    <div class="timeline-line"></div>
                    <div class="flex items-start">
                        <div class="step-number mr-6">2</div>
                        <div class="flex-1 bg-gradient-to-r from-purple-50 to-indigo-50 rounded-xl p-6 card-hover">
                            <h3 class="text-xl font-bold text-gray-800 mb-3">
                                <i class="fas fa-reply text-purple-600 mr-2"></i>
                                第二次挥手（被动关闭方响应）
                            </h3>
                            <div class="grid md:grid-cols-2 gap-4">
                                <div>
                                    <h4 class="font-semibold text-gray-700 mb-2">ACK 确认</h4>
                                    <p class="text-gray-600">被动关闭方回复 ACK 包，确认收到终止请求。</p>
                                </div>
                                <div>
                                    <h4 class="font-semibold text-gray-700 mb-2">状态转换</h4>
                                    <p class="text-gray-600">被动关闭方进入 <span class="font-mono bg-purple-100 px-2 py-1 rounded">CLOSE-WAIT</span> 状态。</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Step 3 -->
                <div class="relative">
                    <div class="timeline-line"></div>
                    <div class="flex items-start">
                        <div class="step-number mr-6">3</div>
                        <div class="flex-1 bg-gradient-to-r from-purple-50 to-indigo-50 rounded-xl p-6 card-hover">
                            <h3 class="text-xl font-bold text-gray-800 mb-3">
                                <i class="fas fa-share text-purple-600 mr-2"></i>
                                第三次挥手（被动关闭方发起）
                            </h3>
                            <div class="grid md:grid-cols-2 gap-4">
                                <div>
                                    <h4 class="font-semibold text-gray-700 mb-2">FIN 包发送</h4>
                                    <p class="text-gray-600">